//
//  SystemVoiceItemView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/1/18.
//

import SwiftUI
import Kingfisher

//公告 item
struct NotifyItemView: View {
    @State var showChoose = false
    @EnvironmentObject var pathManager: PathManager
    @EnvironmentObject var themeManager: ThemeManager
    @StateObject var viewModel = NotifyBaseModel()

    var item: MsgBean
    var body: some View {
        ZStack {
            HStack {
                VStack(alignment: .leading,spacing: 0) {
                    Text(item.statusTitle ?? "公告")
                        .font(.system(size: 14))
                        .foregroundStyle(.bgItemTitle)
                        .padding(.top, 5)
                    Text(item.title ?? "我是标题")
                        .font(.system(size: 14))
                        .foregroundStyle(.bgItemContent)
                        .padding(.top, 5)
                    Text(item.content ?? "内容")
                        .font(.system(size: 14))
                        .foregroundStyle(.bgItemContent2)
                        .padding(.top, 5)
                    if let images = item.imageList, !images.isEmpty {
                        KFImage(URL(string: images[0].url.formatCdn()))
                            .resizable()
                            .placeholder{
                                Image(.appIconLogo)
                                    .resizable()
                                    .scaledToFill()
                                    .frame(width: 50,height: 50)
                                    .cornerRadius(7)
                                    .clipped()
                            }
                            .scaledToFill()
                            .frame(width: 50,height: 50)
                            .cornerRadius(7)
                            .clipped()
                            .padding(.top, 5)
                    }
                    if item.message?.isEmpty == false {
                        Text("作者已留言")
                            .font(.system(size: 12))
                            .foregroundStyle(.red)
                            .padding(.top, 10)
                    }
                    if item.jump != 0 && !isExamine() {
                        //跳转按钮
                        HStack{
                            Spacer()
                            Button(action: {
                                //执行跳转
                                viewModel.jump(type: item.jump)
                            }) {
                                Text(item.btnName ?? "确定")
                                    .foregroundColor(themeManager.themeColor)
                                    .font(.system(size: 16))
                                    .padding(.horizontal,10)
                                    .padding(.vertical,5)
                            }
                            .background(themeManager.themeColor.opacity(0.1))
                            .cornerRadius(8)
                        }
                    }
                }
                Spacer()
                
            }.padding(.vertical,10)
                .padding(.horizontal,15)
                .background(.bgItem)
                .cornerRadius(15)
                .contentShape(Rectangle())
                .onTapGesture {
                    pathManager.jump(type: JumpType.FEEDBACK, data: item)
                }
            
        }.padding(.vertical,7)
            .padding(.horizontal,10)
            .cornerRadius(10)
    }
    
}

//定义预览提供者
struct NotifyItem_Previews: PreviewProvider {
    static var previews: some View {
        //使用预览共享配置
        configurePreviewEnvironment(for: NotifyItemView(item: MsgBean()))
    }
}

